package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Material;
import com.qidianit.mes.pojo.Process;
import com.qidianit.mes.pojo.ProductionOrder;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.Workflow;
import com.qidianit.mes.pro.mapper.*;
import com.qidianit.mes.pro.service.ProScheduleService;
import com.qidianit.mes.pro.vo.ProSchedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/24 -9:29
 */
@Service
public class ProScheduleServiceImpl implements ProScheduleService {
    @Autowired
    private ProductionOrderMapper productionOrderMapper;
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private UnitMapper unitMapper;
    @Autowired
    private WorkflowMapper workflowMapper;
    @Autowired
    private ProcessMapper processMapper;

    @Override
    public Result<PageInfo<ProductionOrder>> queryListByStatus(ProductionOrder productionOrder, int pageNum, int size) {
        //分页
        PageHelper.startPage(pageNum,size);
        List<ProductionOrder> productionOrders = productionOrderMapper.queryListByStatus(productionOrder);
        for (ProductionOrder order : productionOrders) {
            order.setSourceDictData(dictDataMapper.queryByDictDataId(order.getProductionOrderSource()));
            order.setStatusDictData(dictDataMapper.queryByDictDataId(order.getProductionOrderStatus()));
            order.setTypeDictData(dictDataMapper.queryByDictDataId(order.getProductionOrderType()));
            if (customerMapper.selectByCode(order.getCustomerCode())!=null){
                order.setCustomer(customerMapper.selectByCode(order.getCustomerCode()));
            }
            if (materialMapper.queryByCode(order.getMaterialCode()) !=null){
                order.setMaterial(materialMapper.queryByCode(order.getMaterialCode()));
            }
            if(supplierMapper.queryByCode(order.getSupplierCode()) !=null){
                order.setSupplier(supplierMapper.queryByCode(order.getSupplierCode()));
            }
        }
        return Result.success(new PageInfo<>(productionOrders));
    }

    @Override
    public Result<List<Workflow>> getMaterialWorkflowList(String code) {
        List<Workflow> workflows = workflowMapper.queryByMaterialCode(code);
        workflows.forEach(workflow -> {
            List<Process> processes = processMapper.queryBuWorkflowId(workflow.getId());
            workflow.setProcesses(processes);
        });
        return Result.success(workflows);
    }
}
